package com.cheyun.netsalev3.repository

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.order.OrderData
import com.cheyun.netsalev3.bean.order.OrderParam
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class OrderListRepository(val cararrive: String, var map: Map<String, String> = HashMap(),var enable:Boolean = true) : BasePageRepository<OrderData>() {
    fun changeMap(newMap:HashMap<String, String>){
        this.map = newMap
    }
    fun setEanbleLoad(e:Boolean){
        this.enable = e
    }
    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<OrderData>) -> Unit) {
        ApiClient.getInstance().getOrderList(1, 1,cararrive,map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<OrderParam>>() {
                    override fun onSuccess(t: BaseResp<OrderParam>) {
                        t.data?.rows?.apply {
                            total.postValue(t.data!!.total)
                            this.forEach{
                                it.hillenBtn = if(it.appstate == 4 || it.cararrive == 0) 1 else 0
                            }
                            callback(this)
                        }
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }

                })
    }


    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, OrderData>) {

        ApiClient.getInstance().getOrderList(params.key, 1, cararrive,map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<OrderParam>>() {
                    override fun onSuccess(t: BaseResp<OrderParam>) {
                        t.data?.rows?.apply {
                            this.forEach{
//                                it.hillenBtn = if(it.darrivetime.isNotEmpty()  && it.infirmity == 0) 1 else 0
                                it.hillenBtn = if(it.appstate == 4 || it.cararrive == 0) 1 else 0
                            }
                            callback.onResult(this, params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, OrderData>) {

        ApiClient.getInstance().getOrderList(params.key, 1, cararrive,map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<OrderParam>>() {
                    override fun onSuccess(t: BaseResp<OrderParam>) {
                        t.data?.rows?.apply {
                            this.forEach{
                                it.hillenBtn = if(it.appstate == 4 || it.cararrive == 0) 1 else 0
                            }
                            callback.onResult(this, params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    fun confirmCartime(ids:String, darrivetime:String,moth: () -> Unit, fail:()->Unit){
        ApiClient.getInstance().confirmCartime(ids,darrivetime)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {
                        fail()
                    }

                })
    }

    fun addeliver(ids:String,moth: () -> Unit, fail:()->Unit){
        ApiClient.getInstance().addeliver(ids)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {
                        fail()
                    }

                })
    }

    fun servicing(ids:String,moth: () -> Unit, fail:()->Unit){
        ApiClient.getInstance().servicing(ids)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {
                        fail()
                    }

                })
    }

}